<#assign tit>私教课程销售</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  function seldj() {
    var fm = getEl("fm_input");
    var url = "getEarnestList?type=2";
    if(fm.memberCode.value=="") {
      window.alert("请先输入会员卡号!");
      return;
    }
    url += "&memberCode="+fm.memberCode.value;
    openWin(url, 620, 400);
  }
  function seldjOk(vv) {
    var ss = vv.split(",");
    var fm = getEl("fm_input");
    fm.earnestId.value = ss[0];
    fm.earnestAmount.value = ss[1];
    chgfact();
  }

  function getminfo(c) {
    if(c!="") {
      getJson("getMember?cardCode="+c, function(data){
        if(data.ret) {
          var mb = data.data;
          var fm = getEl("fm_input");
          fm.cardCode.value = mb.card;
          fm.memberCode.value = mb.code;
          fm.memberName.value = mb.name;
          fm.contractTime.value = mb.contractEnd;
          if(mb.ptContractEnd && mb.ptContractEnd>=getDateStr(new Date())) {
            var dt = parseDate(mb.ptContractEnd);
            dt.addDay(1);
            fm.beginTime.value = getDateStr(dt);
          } else
            fm.beginTime.value = getDateStr(new Date());
          chgpt();
        } else {
          window.alert(data.msg);
          cldc();
        }
      });
    } else {
      cldc();
    }
  }

  function cldc() {
    var fm = getEl("fm_input");
    fm.cardCode.value = "";
    fm.memberCode.value = "";
    fm.memberName.value = "";
    fm.contractTime.value = "";
    fm.beginTime.value = "";
    fm.endTime.value = "";
  }

  var _pr = null;
  function chgpt() {
    var fm = getEl("fm_input");
    var pt = fm.ptCode.value;
    var cc = fm.coachCode.value;
    var n = fm.quantity.value;
    if(pt!="" && cc!="" && n!="") {
      var url = "getPrice?ptCode="+pt+"&coachCode="+cc+"&quantity="+n;
      getJson(url, function(data){
        if(data.ret) {
          setpr(data.data);
        } else {
          window.alert(data.msg);
          setpr(null);
        }
      });
    } else {
      setpr(null);
    }
  }

  function setpr(pr) {
    var fm = getEl("fm_input");
    if(pr) {
      fm.price.value = formatMoney(pr.priceMin)+'-'+formatMoney(pr.priceMax);
      //fm.beginTime.value = pr.beginTime;
      var dt = parseDate(fm.beginTime.value);
      dt.addDay(pr.validDays);
      fm.endTime.value = getDateStr(dt);
    } else {
      fm.price.value = "";
      //fm.beginTime.value = "";
      fm.endTime.value = "";
    }
    _pr = pr;
  }

  function chgdt1() {
    var fm = getEl("fm_input");
    var dt = parseDate(fm.beginTime.value);
    if(dt==null) return;
    dt.addDay(_pr.validDays);
    fm.endTime.value = getDateStr(dt);
  }

  function chgfact() {
    var fm = getEl("fm_input");
    if(fm.factPrice.value=="") {
      fm.payAmount.value = "";
      return;
    }
    var m1 = parseFloat(fm.factPrice.value);
    if(isNaN(m1)) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    fm.factPrice.value = formatMoney(m1);
    fm.payAmount.value = formatMoney(m1-m2);
  }

  function chgpaym() {
    var fm = getEl("fm_input");
    if(fm.payAmount.value=="") {
      fm.factPrice.value = "";
      return;
    }
    var m1 = parseFloat(fm.payAmount.value);
    if(isNaN(m1)) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    fm.factPrice.value = formatMoney(m1+m2);
    fm.payAmount.value = formatMoney(m1);
  }

  function save(fm) {
    if(!fm) fm = getEl("fm_input");
    if(fm.memberCode.value=="") {
      window.alert("请输入会员卡号");
      return false;
    }

    if(_pr==null) {
      window.alert("请输入课程和数量");
      return false;
    }

    var price = parseFloat(fm.factPrice.value);
    if(price<_pr.priceMin || price>_pr.priceMax) {
      window.alert("实际售价必须按指导价范围");
      return false;
    }

    var mm = fm.payAmount.value;
    post(fm, function(){
      openWin("getPay?money="+mm, 500, 400);
      return false;
    });
    return false;
  }
  function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    postForm(fm.action, fm, saveRet);
  }
  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) getEl("fm_input").reset(); //window.location = "print?id="+data.data;
  }
</script>
<div style="width:600px;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value="">
    <div class="kbox">
      <table class="input w4">
        <tr>
          <th width="85">会员卡号</th>
          <td width="160"><input type="text" name="cardCode" value="" onchange="getminfo(this.value);" maxlength="20"></td>
          <th width="85">会员编号</th>
          <td width="160"><input type="text" name="memberCode" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>会员姓名</th>
          <td><input type="text" name="memberName" value="" maxlength="10" class="disable" readonly></td>
          <th>合同终止日</th>
          <td><input type="text" name="contractTime" value="" maxlength="10" class="disable" readonly></td>
        </tr>
        <tr>
          <th>定金编号</th>
          <td><input type="text" name="earnestId" value="" style="width:90px;" class="disable" readonly>
            <input type="button" class="opwin" value="..." onclick="seldj();"/></td>
          <th>定金金额</th>
          <td><input type="text" name="earnestAmount" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>私教课程</th>
          <td colspan="3"><select name="ptCode" onchange="chgpt();" class="w2" req="私教课程"><option value="">--请选择--</option><#if itemList??><#list itemList as t><option value="${t.code!}">${t.code!} - ${t.chName!}</option></#list></#if></select></td>
        </tr>
        <tr>
          <th>上课教练</th>
          <td colspan="3"><select name="coachCode" onchange="chgpt();" class="w2" req="上课教练"><option value="">--请选择--</option><#if coachList??><#list coachList as t><option value="${t.code!}">${t.code!} - ${t.chName!} - <@value type="coachLevel" key="${t.coachLevel!}"/></option></#list></#if></select></td>
        </tr>
        <tr>
          <th>购买数量</th>
          <td><input type="text" name="quantity" value="" onchange="chgpt();" maxlength="6" req="购买数量"></td>
          <th>指导价范围</th>
          <td><input type="text" name="price" value="" class="disable" readonly></td>
        </tr>

        <tr>
          <th>开始日期</th>
          <td><input type="text" name="beginTime" value="" class="date disable" maxlength="10" readonly onchange="chgdt1();" onfocus="if(!this.readOnly)cal(this);"></td>
          <th>结束日期</th>
          <td><input type="text" name="endTime" value="" class="date disable" maxlength="10" readonly></td>
        </tr>
        <tr>
          <th>实际售价</th>
          <td><input type="text" name="factPrice" value="" onchange="chgfact();" req="实际售价"></td>
          <th>应收金额</th>
          <td><input type="text" name="payAmount" value="" onchange="chgpaym();"></td>
        </tr>
        <tr>
          <th>备注</th>
          <td colspan="3"><textarea name="note"></textarea></td>
        </tr>
      </table>
    </div>
    <div class="button"><input type="button" value="确认并提交" onclick="save();"></div>
  </form>
</div>

</@page.html>